#include "src/core/xmlstreamwriter.h"
#include <QtCore/QFile>
#include <QtCore/QRegExp>
-//#include <QtCore/QTextCodec>
-// #include <QtXml/QXmlStreamAttributes>
+#include <QtCore/QDateTime>
#include <QtCore/QDebug>
}
gpsbabel::DateTime
-xml_parse_time(const char* cdatastr)
+xml_parse_time(const char* cdatastr)
{
int off_hr = 0;
int off_min = 0;
if (offsetstr) {
/* positive offset; parse it */
*offsetstr = '\0';
- sscanf(offsetstr+1, "%d:%d", &off_hr, &off_min);
+ sscanf(offsetstr + 1, "%d:%d", &off_hr, &off_min);
} else {
offsetstr = strchr(timestr, 'T');
if (offsetstr) {
if (offsetstr) {
/* negative offset; parse it */
*offsetstr = '\0';
- sscanf(offsetstr+1, "%d:%d",
- &off_hr, &off_min);
+ sscanf(offsetstr + 1, "%d:%d", &off_hr, &off_min);
off_sign = -1;
}
}
}
int year = 0, mon = 0, mday = 0, hour = 0, min = 0, sec = 0;
- sscanf(timestr, "%d-%d-%dT%d:%d:%d",
- &year,
- &mon,
- &mday,
- &hour,
- &min,
- &sec);
- QDate date(year, mon, mday);
- QTime time(hour, min, sec);
-
- // Fractional part of time.
- if (fsec) {
- time = time.addMSecs(lround(fsec * 1000));
- }
-
- // Any offsets that were stuck at the end.
- time = time.addSecs(-off_sign*off_hr*3600 - off_sign*off_min*60);
-
- QDateTime dt(date, time, Qt::UTC);
- xfree(timestr);
+ QDateTime dt;
+ int res = sscanf(timestr, "%d-%d-%dT%d:%d:%d", &year, &mon, &mday, &hour,
+ &min, &sec);
+ if (res > 0) {
+ QDate date(year, mon, mday);
+ QTime time(hour, min, sec);
+
+ // Fractional part of time.
+ if (fsec) {
+ time = time.addMSecs(lround(fsec * 1000));
+ }
+
+ // Any offsets that were stuck at the end.
+ time = time.addSecs(-off_sign * off_hr * 3600 - off_sign * off_min * 60);
+
+ xfree(timestr);
+ dt = QDateTime(date, time, Qt::UTC);
+ } else {
+ dt = QDateTime();
+ }
return dt;
}
gpx_wr_init(const char* fname)
{
mkshort_handle = NULL;
- ofd = gbfopen(fname, "w", MYNAME);
+ // QFile requires binary mode on Windows.
+ ofd = gbfopen(fname, "wb", MYNAME);
oqfile.open(ofd->handle.std, QIODevice::WriteOnly);
// This is ia bit of a lie. QXMLStreamWriter will pass everything
void wpt_time(const char* args, const char** unused)
{
- wpt_tmp->creation_time = xml_parse_time(args);
+ wpt_tmp->SetCreationTime(xml_parse_time(args));
}
void wpt_coord(const char* args, const char** attrv)
{
char u = 's';
waypt_init_bounds(&kml_bounds);
- kml_time_min = 0;
- kml_time_max = 0;
+ kml_time_min = QDateTime();
+ kml_time_max = QDateTime();
if (opt_units) {
u = tolower(opt_units[0]);
kml_td(hwriter, QString("Longitude: %1 ").arg(QString::number(pt->longitude, 'f', 6)));
kml_td(hwriter, QString("Latitude: %1 ").arg(QString::number(pt->latitude, 'f', 6)));
+
if (kml_altitude_known(pt)) {
kml_td(hwriter, QString("Altitude: %1 %2 ").arg(QString::number(alt, 'f', 3)).arg(alt_units));
}
+
if (pt->heartrate) {
kml_td(hwriter, QString("Heart rate: %1 ").arg(QString::number(pt->heartrate)));
}
+
if (pt->cadence) {
kml_td(hwriter, QString("Cadence: %1 ").arg(QString::number(pt->cadence)));
}
+
/* Which unit is this temp in? C? F? K? */
if WAYPT_HAS(pt, temperature) {
kml_td(hwriter, QString("Temperature: %1 ").arg(QString::number(pt->temperature, 'f', 1)));
}
+
if WAYPT_HAS(pt, depth) {
const char* depth_units;
double depth = fmt_distance(pt->depth, &depth_units);
kml_td(hwriter, QString("Depth: %1 %2 ").arg(QString::number(depth, 'f', 1)).arg(depth_units));
}
+
if WAYPT_HAS(pt, speed) {
const char* spd_units;
double spd = fmt_speed(pt->speed, &spd_units);
kml_td(hwriter, QString("Speed: %1 %2 ").arg(QString::number(spd, 'f', 1)).arg(spd_units));
}
+
if WAYPT_HAS(pt, course) {
kml_td(hwriter, QString("Heading: %1 ").arg(QString::number(pt->course, 'f', 1)));
}
+
/* This really shouldn't be here, but as of this writing,
* Earth can't edit/display the TimeStamp.
*/
- if (pt->GetCreationTime()) {
+ if (pt->GetCreationTime().isValid()) {
QString time_string = pt->CreationTimeXML();
if(!time_string.isEmpty()) {
kml_td(hwriter, QString("Time: %1 ").arg(time_string));
static void kml_recompute_time_bounds(const waypoint* waypointp)
{
- if (waypointp->GetCreationTime() && (waypointp->GetCreationTime() < kml_time_min)) {
- kml_time_min = waypointp->GetCreationTime();
+ gpsbabel::DateTime t = waypointp->GetCreationTime();
+ if (t == 0)
+ return;
+
+ if (!t.isValid())
+ return;
+
+ if (!kml_time_min.isValid() || (t > 0 && t < kml_time_min)) {
+ kml_time_min = t;
}
- if (waypointp->GetCreationTime() > kml_time_max) {
- kml_time_max = waypointp->GetCreationTime();
- if (kml_time_min == 0) {
- kml_time_min = waypointp->GetCreationTime();
- }
+
+ if (!kml_time_max.isValid() || t > kml_time_max) {
+ kml_time_max = t;
}
}
root = fs_gpx->tag;
curlog = xml_findfirst(root, "groundspeak:log");
while (curlog) {
- time_t logtime = 0;
-
// Unless we have a broken GPX input, these logparts
// branches will always be taken.
logpart = xml_findfirst(curlog, "groundspeak:type");
logpart = xml_findfirst(curlog, "groundspeak:date");
if (logpart) {
- logtime = xml_parse_time(logpart->cdata);
- struct tm* logtm = localtime(&logtime);
- if (logtm) {
+ gpsbabel::DateTime t = xml_parse_time(logpart->cdata);
+ if (t.isValid()) {
char* temp;
xasprintf(&temp,
" %04d-%02d-%02d",
- logtm->tm_year+1900,
- logtm->tm_mon+1,
- logtm->tm_mday);
+ t.date().year(),
+ t.date().month(),
+ t.date().day()),
r = xstrappend(r, temp);
xfree(temp);
}
// Timestamp
kml_output_timestamp(waypointp);
- if (waypointp->creation_time) {
+ if (waypointp->GetCreationTime().isValid()) {
strcpy(date_placed,
qPrintable(waypointp->GetCreationTime().toString("dd-MMM-yyyy")));
} else {
QUEUE_FOR_EACH(&header->waypoint_list, elem, tmp) {
waypoint* tpt = (waypoint*)elem;
- if (tpt->creation_time) {
+ // FIXME: implicit time_t test here.
+ if (tpt->GetCreationTime().isValid() && tpt->GetCreationTime() > 0) {
points_with_time++;
if (points_with_time >= 2) {
return 1;
QUEUE_FOR_EACH(&header->waypoint_list, elem, tmp) {
waypoint* tpt = (waypoint*)elem;
-
- if (tpt->GetCreationTime()) {
+ // FIXME: implicit time_t test here.
+ if (tpt->GetCreationTime().isValid() && tpt->GetCreationTime() > 0) {
QString time_string = tpt->CreationTimeXML();
writer->writeOptionalTextElement("when", time_string);
} else {
writer->writeStartElement("LookAt");
- if (kml_time_min || kml_time_max) {
+ if (kml_time_min.isValid() || kml_time_max.isValid()) {
writer->writeStartElement("gx:TimeSpan");
if (kml_time_min.isValid()) {
writer->writeTextElement("begin", kml_time_min.toPrettyString());
}
- if (kml_time_max) {
+ if (kml_time_max.isValid()) {
gpsbabel::DateTime time_max;
// In realtime tracking mode, we fudge the end time by a few minutes
// to ensure that the freshest data (our current location) is contained